<%--
    @Author: xzy 1453077527@qq.com
    @Date: 2025-08-26
    @LastEditors: xzy 1453077527@qq.com
    @LastEditTime: 2025-08-27 09:57:07
    @Description:
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<li class="nav-item">
    <c:choose>
        <%--有子菜单 --%>
        <c:when test="${not empty menu.children}">
            <a class="nav-link has-children collapsed" data-bs-toggle="collapse" href="#submenu-${menu.id}">
                <i class="bi ${menu.icon}"></i>${menu.name}
            </a>
            <div class="collapse submenu" id="submenu-${menu.id}">
                <ul class="nav flex-column">
                    <c:forEach items="${menu.children}" var="child">
                        <%-- 计算子菜单的完整路径 --%>
                        <c:set var="childFullPath">
                            <c:choose>
                                <c:when test="${not empty currentPath}">
                                    ${currentPath}/${child.path}
                                </c:when>
                                <c:otherwise>
                                    ${menu.path}/${child.path}
                                </c:otherwise>
                            </c:choose>
                        </c:set>

                        <%-- 保存当前的变量状态 --%>
                        <c:set var="savedMenu" value="${menu}" />
                        <c:set var="savedCurrentPath" value="${currentPath}" />

                        <%-- 设置子菜单的变量 --%>
                        <c:set var="menu" value="${child}" scope="request"/>
                        <c:set var="currentPath" value="${childFullPath}" scope="request"/>

                        <%-- 递归生成子菜单 --%>
                        <jsp:include page="sidebar.jsp"/>

                        <%-- 恢复变量状态 --%>
                        <c:set var="menu" value="${savedMenu}" scope="request"/>
                        <c:set var="currentPath" value="${savedCurrentPath}" scope="request"/>
                    </c:forEach>
                </ul>
            </div>
        </c:when>
        <%--无子菜单--%>
        <c:otherwise>
            <%-- 构建完整的URL路径 --%>
            <c:set var="fullPath">
                <c:choose>
                    <c:when test="${not empty currentPath}">
                        ${currentPath}
                    </c:when>
                    <c:otherwise>
                        ${menu.path}
                    </c:otherwise>
                </c:choose>
            </c:set>

            <a class="nav-link" href="<c:url value="/${fullPath}">
                    <c:if test="${not empty pageIndex}">
                        <c:param name="pageIndex" value="${pageIndex}" />
                    </c:if>
                </c:url>">
                <i class="bi ${menu.icon}"></i>${menu.name}
            </a>
        </c:otherwise>
    </c:choose>
</li>
